package gui;

import org.Consts;
import org.Globals;
import org.json.JSONException;
import org.json.JSONObject;

import CustomUI.BrushPropertiesDialog;
import CustomUI.SaveView;
import CustomUI.ShapePropertiesDialog;
import CustomUI.SketchView;
import CustomUI.TransformationToolsView;
import CustomUI.ydButton;
import CustomUI.ydCustomColorPicker;
import CustomUI.ydScrollView;
import DTO.DrawingPlaneDTO;
import DTO.ObjectDTO;
import DTO.Properties;
import GameTools.ydPoint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ydev.graphics.animations.R;

public class AddEditObjects extends Activity {

	SketchView sketchView;
	
	SaveView saveView;

	ydScrollView sv;

	TransformationToolsView tTools;

	public LinearLayout linearLayoutDrawingTools;
	public LinearLayout linearLayoutDrawingToolShapes;
	public LinearLayout linearLayoutArrows;

	ydButton ydButtonShowDrawingTools, ydButtonBrush, ydButtonErase,
			ydButtonShape, ydButtonText, ydButtonColor, ydButtonUndo,
			ydButtonBack;

	ydButton ydButtonMove, ydButtonRotate, ydButtonScale;
	
	ydButton ydButtonSave;
	
	Properties brushProp=new Properties(Consts.DRAWING_ACTIONS_BRUSH);
	Properties shapeProp=new Properties(Consts.DRAWING_ACTIONS_Shape);


	
	ydCustomColorPicker ydCCP;//= new ydCustomColorPicker();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_edit_objects);

		sketchView = (SketchView) findViewById(R.id.sketchView1);
		saveView=(SaveView)findViewById(R.id.saveView1);
		sv = (ydScrollView) findViewById(R.id.ydScrollView1);
		tTools = (TransformationToolsView) findViewById(R.id.transformationToolsView1);

		linearLayoutDrawingTools = (LinearLayout) findViewById(R.id.linearLayoutDrawingTools);
		linearLayoutDrawingToolShapes = (LinearLayout) findViewById(R.id.linearLayoutDrawingToolShapes);
		linearLayoutArrows = (LinearLayout) findViewById(R.id.linearLayoutArrows);

		init();

	}

	public void init() {
	
		sv.init();
		ydCCP= new ydCustomColorPicker(this);
		ydButton btn=(ydButton)ydCCP.findViewById(R.id.ydButtonOK);
		btn.addOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sketchView.setColor(ydCCP.getColor());
				ydCCP.hide();
				
			}
		});
		
	
		tTools.addOnTouchListener(new View.OnTouchListener() {
	
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				ydPoint shift = tTools.getShift();
	
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
				//	sketchView.deltaTransform(shift, MotionEvent.ACTION_DOWN);
				} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
					//sketchView.deltaTransform(shift, MotionEvent.ACTION_MOVE);
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					//sketchView.deltaTransform(shift, MotionEvent.ACTION_UP);
					drawingToolClicked(ydButtonBrush);
					tTools.setVisibility(View.GONE);
				}
				
				tTools.applyOutlineTransform(shift);
				
				return true;
			}
		});
	
		sketchView.setOnTouchListener(new View.OnTouchListener() {
	
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (sketchView.currentAction
						.equals(Consts.DRAWING_ACTIONS_Shape)) {
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						showArrows();
					} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
						showArrows();
					} else if (event.getAction() == MotionEvent.ACTION_UP) {
						showArrows();
					}
				}
				return false;
			}
	
			public void showArrows() {
				linearLayoutDrawingToolShapes.setVisibility(View.GONE);
				sv.setVisibility(View.GONE);
	
				linearLayoutArrows.setVisibility(View.VISIBLE);
			}
		});
	
		/*colorChangedListener = new OnColorChangedListener() {
	
			@Override
			public void colorChanged(int color) {
				sketchView.setColor(color);
			}
		};
		cp = new ColorPickerDialog(this, colorChangedListener, Color.WHITE);*/
	
		ydButtonShowDrawingTools = (ydButton) findViewById(R.id.ydButtonShowDrawingTools);
		ydButtonShowDrawingTools
				.addOnLongClickListener(new View.OnLongClickListener() {
	
					@Override
					public boolean onLongClick(View v) {
						Toast.makeText(getApplicationContext(), "msg msg",
								Toast.LENGTH_LONG).show();
						return true;
					}
	
				});
	
		showHideDrawingTools(ydButtonShowDrawingTools);
	
		ydButtonBrush = (ydButton) findViewById(R.id.ydButtonBrush);
		
		final BrushPropertiesDialog BPD= new BrushPropertiesDialog(this);
		ydButtonBrush.addOnLongClickListener(new View.OnLongClickListener() {
	
			@Override
			public boolean onLongClick(View v) {

	
				
				BPD.show();
				return true;
			}
	
		});
		
		btn=(ydButton)BPD.findViewById(R.id.ydButtonOK);
		btn.addOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sketchView.setBrushThickness(BPD.getBrushWidth());
				sketchView.setFill(BPD.getFillProperty());
				BPD.hide();
				
			}
		});
	
		ydButtonShape = (ydButton) findViewById(R.id.ydButtonShape);
		ydButtonShape.addOnLongClickListener(new View.OnLongClickListener() {
	
			@Override
			public boolean onLongClick(View v) {
				/*
				 * Toast.makeText(getApplicationContext(), "msg msg",
				 * Toast.LENGTH_LONG).show();
				 */
	
				ydButtonShape.setSelected(true);
	
				// btn.setSelected(selected);
				linearLayoutDrawingToolShapes.setVisibility(View.VISIBLE);
				return true;
			}
	
		});
		
		final ShapePropertiesDialog SPD= new ShapePropertiesDialog(this);
		ydButtonShape.addOnLongClickListener(new View.OnLongClickListener() {
	
			@Override
			public boolean onLongClick(View v) {

	
				
				SPD.show();
				return true;
			}
	
		});
		
		btn=(ydButton)SPD.findViewById(R.id.ydButtonOK);
		btn.addOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
			//	sketchView.setBrushThickness(BPD.getBrushWidth());
			//	sketchView.setFill(BPD.getFillProperty());
				SPD.hide();
				
			}
		});
	
		/*ydButtonShape.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (ydButtonShape.isSelected() == false)
					linearLayoutDrawingToolShapes.setVisibility(View.GONE);
			}
		});*/
	
		ydButton ydButtonOtherShapes = (ydButton) findViewById(R.id.ydButtonOtherShape1);
		changeShapeClicked(ydButtonShape, ydButtonOtherShapes);
	
		ydButtonOtherShapes = (ydButton) findViewById(R.id.ydButtonOtherShape2);
		changeShapeClicked(ydButtonShape, ydButtonOtherShapes);
	
		ydButtonOtherShapes = (ydButton) findViewById(R.id.ydButtonOtherShape3);
		changeShapeClicked(ydButtonShape, ydButtonOtherShapes);
	
		ydButtonText = (ydButton) findViewById(R.id.ydButtonText);
	
		ydButtonErase = (ydButton) findViewById(R.id.ydButtonErase);
	
		ydButtonColor = (ydButton) findViewById(R.id.ydButtonColor);
		ydButtonColor.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//cp.show();
				
				ydCCP.show();
			}
		});
	
		ydButtonUndo = (ydButton) findViewById(R.id.ydButtonUndo);
		ydButtonUndo.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				sketchView.setScale(new ydPoint(1.5f,1.5f));
				
			}
		});
	
		ydButtonBack = (ydButton) findViewById(R.id.ydButtonBack);
		showHideDrawingTools(ydButtonBack);
	
		ydButtonMove = (ydButton) findViewById(R.id.ydButtonMove);
		translationToolClicked(ydButtonMove);
	
		ydButtonRotate = (ydButton) findViewById(R.id.ydButtonRotate);
		translationToolClicked(ydButtonRotate);
	
		ydButtonScale = (ydButton) findViewById(R.id.ydButtonScale);
		translationToolClicked(ydButtonScale);
		
		ydButtonSave=(ydButton)findViewById(R.id.ydButtonSave);
		ydButtonSave.addOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//sketchView.save();
				if(ydButtonSave.isSelected()){
					sketchView.setEnabled(false);
					saveView.setVisibility(View.VISIBLE);
				}
				else if(!ydButtonSave.isSelected()){
					sketchView.setEnabled(false);
					saveView.setVisibility(View.GONE);
				}
				
			}
		});
	
		setActionClicked();
	
		setArrows();
	
		Bundle extras = getIntent().getExtras();
	
		if (extras.containsKey(Consts.DTO_PK)) {
			int object_id = extras.getInt(Consts.DTO_PK);
	
			if (object_id != -1) {
				loadProject(object_id);
			}
		}
	}

	private void loadProject(int object_id) {
		ObjectDTO obj = new ObjectDTO(object_id, Globals.sqlDB);
		sketchView.drawingPlanes = DrawingPlaneDTO.getDrawingPlanesInDatabase(
				Globals.sqlDB, obj);
	}

	private void translationToolClicked(final ydButton btn) {
		btn.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				JSONObject obj = null;
				try {
					obj = new JSONObject((String) btn.getTag());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				tTools.setVisibility(View.VISIBLE);
				tTools.setAction(obj.optString(Consts.VIEW_TAGS_ACTION));
				tTools.setDrawingPlane(sketchView.getDrawingPlane());

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}

	public void actionClicked(final ydButton btn) {
		btn.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				drawingToolClicked(btn);
			}
		});

	}

	public void drawingToolClicked(ydButton btn) {
		JSONObject obj = null;
		try {
			obj = new JSONObject((String) btn.getTag());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (obj.optString(Consts.VIEW_TAGS_TOGGLE).equals("true")) {
			ydButtonBrush.setSelected(false);
			ydButtonShape.setSelected(false);
			ydButtonText.setSelected(false);
			ydButtonColor.setSelected(false);
			ydButtonErase.setSelected(false);

			ydButtonMove.setSelected(false);
			ydButtonRotate.setSelected(false);
			ydButtonScale.setSelected(false);

			btn.setSelected(true);

			sketchView.setAction(obj.optString(Consts.VIEW_TAGS_ACTION));
		}
	}

	public void setArrows() {
		ydButton btn;
		btn = (ydButton) (findViewById(R.id.ydButtonNW));
		setDirectionClickedListener(btn);
		btn = (ydButton) (findViewById(R.id.ydButtonN));
		setDirectionClickedListener(btn);
		btn = (ydButton) (findViewById(R.id.ydButtonNE));
		setDirectionClickedListener(btn);
		btn = (ydButton) (findViewById(R.id.ydButtonW));
		setDirectionClickedListener(btn);
		btn = (ydButton) (findViewById(R.id.ydButtonE));
		setDirectionClickedListener(btn);
		btn = (ydButton) (findViewById(R.id.ydButtonSW));
		setDirectionClickedListener(btn);
		btn = (ydButton) (findViewById(R.id.ydButtonS));
		setDirectionClickedListener(btn);
		btn = (ydButton) (findViewById(R.id.ydButtonSE));
		setDirectionClickedListener(btn);

		btn = (ydButton) (findViewById(R.id.ydButtonCheck));

		btn.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				// linearLayoutDrawingToolShapes.setVisibility(View.VISIBLE);
				sv.setVisibility(View.VISIBLE);

				linearLayoutArrows.setVisibility(View.GONE);

				sketchView.doneAction();
			}
		});

		btn = (ydButton) (findViewById(R.id.ydButtonX));

		btn.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				// linearLayoutDrawingToolShapes.setVisibility(View.VISIBLE);
				sv.setVisibility(View.VISIBLE);

				linearLayoutArrows.setVisibility(View.GONE);

				sketchView.cancelAction();
			}
		});

	}

	public void setDirectionClickedListener(ydButton btn) {

		btn.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				JSONObject obj = null;

				try {
					obj = new JSONObject((String) v.getTag());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				String direction = obj.optString(Consts.VIEW_TAGS_DIRECTION);

				sketchView.directionClicked(direction);

			}
		});
	}

	public void setActionClicked() {
		actionClicked(ydButtonBrush);
		actionClicked(ydButtonShape);
		actionClicked(ydButtonText);
		actionClicked(ydButtonColor);
		actionClicked(ydButtonErase);
		actionClicked(ydButtonMove);
		actionClicked(ydButtonRotate);
		actionClicked(ydButtonScale);
	}

	public void showHideDrawingTools(ydButton btn) {
		btn.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (sv.getVisibility() == View.VISIBLE) {
					linearLayoutDrawingToolShapes.setVisibility(View.GONE);
					sv.setVisibility(View.GONE);
				} else {
					sv.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	public void changeShapeClicked(final ydButton current, final ydButton chosen) {
		chosen.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				JSONObject currentObj = null;
				try {
					currentObj = new JSONObject((String) current.getTag());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				JSONObject chosenObj = null;
				try {
					chosenObj = new JSONObject((String) chosen.getTag());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				int currentID = getShapeID(currentObj
						.optString(Consts.VIEW_TAGS_SHAPE));
				int chosenID = getShapeID(chosenObj
						.optString(Consts.VIEW_TAGS_SHAPE));

				try {
					currentObj.put(Consts.VIEW_TAGS_TOGGLE, "false");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					chosenObj.put(Consts.VIEW_TAGS_TOGGLE, "true");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				current.setTag(chosenObj.toString());
				current.setBackgroundResource(chosenID);

				chosen.setBackgroundResource(currentID);
				chosen.setTag(currentObj.toString());

				drawingToolClicked(current);

				sketchView.setShape(chosenObj.optString(Consts.VIEW_TAGS_SHAPE));

				linearLayoutDrawingToolShapes.setVisibility(View.GONE);
			}

			private int getShapeID(String optString) {
				if (optString.equals(Consts.DRAWING_SHAPE_CIRCLE)) {
					return R.drawable.drawing_tools_shape_circle;
				} else if (optString.equals(Consts.DRAWING_SHAPE_RECTANGLE)) {
					return R.drawable.drawing_tools_shape_rectangle;
				} else if (optString.equals(Consts.DRAWING_SHAPE_LINE)) {
					return R.drawable.drawing_tools_shape_line;
				} else if (optString.equals(Consts.DRAWING_SHAPE_ARC)) {
					return R.drawable.drawing_tools_shape_arc;
				}
				return 0;
			}
		});
	}

	public void directionClicked(View v) {
		JSONObject obj = null;

		try {
			obj = new JSONObject((String) v.getTag());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String direction = obj.optString(Consts.VIEW_TAGS_DIRECTION);

		sketchView.directionClicked(direction);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	    	
	        this.finish();
	        return true;
	    }

	    return super.onKeyDown(keyCode, event);
	}

}
